package com.heu.blood.storge.controller;

import com.heu.blood.common.aop.ControllerLogAnnotation;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.R;
import com.heu.blood.match.dto.req.BloodInformationQueryPageReqDTO;
import com.heu.blood.match.entity.BloodInformationEntity;
import com.heu.blood.storge.dao.BloodOutStorgeDao;
import com.heu.blood.storge.dto.BloodOutStorageDTO;
import com.heu.blood.storge.entity.BloodOutStorgeEntity;
import com.heu.blood.storge.service.BloodOutStorgeService;
import com.heu.blood.storge.vo.BloodUrgentOutStorageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 *
 *
 * @author lyj
 * @email lyj@heu.com
 * @date 2023-06-16 00:33:53
 */
@Slf4j
@RestController
@RequestMapping("storge/bloodoutstorge")
public class BloodOutStorgeController {
    @Autowired
    private BloodOutStorgeService bloodOutStorgeService;

    /**
     * 分页查询出库血液信息
     */
    @RequestMapping("/list")
    @ControllerLogAnnotation(module = "血液出库", operate = "分页查询血液出库信息")
    public R list(@RequestBody BloodOutStorageDTO requestParam){
        PageUtils page = bloodOutStorgeService.queryPage(requestParam);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ControllerLogAnnotation(module = "血液出库", operate = "根据id查询血液出库信息")
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        BloodOutStorgeEntity bloodOutStorge = bloodOutStorgeService.getById(id);

        return R.ok().put("data", bloodOutStorge);
    }

    /**
     * 保存
     */
    @ControllerLogAnnotation(module = "血液出库", operate = "插入血液出库信息")
    @RequestMapping("/save")
    public R save(@RequestBody BloodOutStorgeEntity bloodOutStorge){
        bloodOutStorgeService.save(bloodOutStorge);

        return R.ok();
    }

    /**
     * 这是实际用的出库接口
     */
    @ControllerLogAnnotation(module = "血液出库", operate = "插入血液出库信息")
    @RequestMapping("/newSave")
    public R newSave(@RequestBody BloodOutStorgeEntity bloodOutStorge){
        bloodOutStorgeService.newSave(bloodOutStorge);

        return R.ok();
    }

    /**
     * 修改
     */
    @ControllerLogAnnotation(module = "血液出库", operate = "更新血液出库信息")
    @RequestMapping("/update")
    public R update(@RequestBody BloodOutStorgeEntity bloodOutStorge){
        bloodOutStorgeService.updateById(bloodOutStorge);

        return R.ok();
    }

    /**
     * 删除
     */
    @ControllerLogAnnotation(module = "血液出库", operate = "根据id删除血液出库信息")
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        bloodOutStorgeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    //血液出库
    @RequestMapping("/bloodOutStorge")
    @ControllerLogAnnotation(module = "血液出库", operate = "根据血液id进行血液出库操作")
    public R bloodOutStorge(@RequestParam Map<String, Object> params){
        log.info("params : {}", params); // 打印参数
        if (bloodOutStorgeService.bloodOutStorge(params)){

            return R.ok("血液出库成功");
        }
        else {
            return R.ok("血液出库失败");
        }

    }

    @PostMapping("/urgentOutStorage")
    @ControllerLogAnnotation(module = "血液出库", operate = "紧急出库")
    public R urgentOutStorage(@RequestBody BloodUrgentOutStorageVo bloodUrgentOutStorageVo) {

        int res = bloodOutStorgeService.urgentOutStorage(bloodUrgentOutStorageVo);
        if (res == 0) {
            return R.ok("紧急出库成功");
        } else {
            return R.error("紧急出库失败");
        }
    }
}
